package com.miracle.mango.entity.role;

import com.miracle.mango.entity.user.User;
import com.miracle.mango.entity.user.UserMapper;
import com.miracle.mango.util.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.UUID;

/**
 * @description:
 * @author: wangxf
 * @date: Created in 2020/8/22 23:33
 */
@Service
@Validated
public class RoleServive {

    @Autowired
    private RoleMapper roleMapper;

    public int save(@Valid Role role) {
        if(StringUtils.isEmpty(role.getId())) {
            role.setId(UUID.randomUUID().toString());
        }
        return roleMapper.save(role);
    }

    public int savePermission(String roleId, String permissionId) {
        String id = UUID.randomUUID().toString();
        return roleMapper.savePermission(id, roleId, permissionId);
    }

    public int update(Role role) {
        Role saved = roleMapper.getById(role.getId());
        if (null == saved) {
            throw new ServiceException("角色不存在!");
        }
        BeanUtils.copyProperties(role, saved);
        return roleMapper.update(role);
    }

    public int removeById(String id) {
        return roleMapper.removeById(id);
    }

}
